package com.example.wwk.graduationpractice.Activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.example.wwk.graduationpractice.AsyncTask.FindPwdAsyncTask;
import com.example.wwk.graduationpractice.Bean.FindEmailBean;
import com.example.wwk.graduationpractice.Globalutils.ActivityCollector;
import com.example.wwk.graduationpractice.Globalutils.CustomDialogUtils;
import com.example.wwk.graduationpractice.Globalutils.PromptShowUtils;
import com.example.wwk.graduationpractice.NetDataUtils.FindPwdUtils;
import com.example.wwk.graduationpractice.R;

public class FindPasswordActivity extends AppCompatActivity implements View.OnClickListener {
    //back
    private ImageView back;
    //editText
    private EditText email;
    private EditText username;
    //获得EditText的内容
    private String Email;
    private String UserName;
    //button send
    private Button send;
    //加载对话框
    private CustomDialogUtils progressDialog;
    //该email是否存在
    private int emailCount;
    //handler
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case FindPwdUtils.GET_EMAIL:
                    //网络请求查询email后
                    FindEmailBean findEmailBean = (FindEmailBean) msg.obj;
                    emailCount = findEmailBean.getEmailCount();
                    if (emailCount == 0) {  //该用户的email不存在
                        progressDialog.dismiss();
                        PromptShowUtils.showDialog2("提示", "您输入的注册邮箱不存在！", FindPasswordActivity.this);
                    } else if (emailCount == 1) {//用户对应的注册邮箱存在
                        FindPwdAsyncTask.findPwd(Email, handler);   //处理找回密码逻辑
                    }
                    break;
                //用户对应的注册邮箱存在 ,处理后的返回结果
                case FindPwdAsyncTask.findPwdOk:    //找回密码成功
                    progressDialog.dismiss();
                    PromptShowUtils.showDialog3("提示", "用户信息已发送到您的注册邮箱！", FindPasswordActivity.this);
                    break;
                case FindPwdAsyncTask.findPwdOff:   //找回密码失败
                    progressDialog.dismiss();
                    PromptShowUtils.showDialog2("提示", "发送失败！", FindPasswordActivity.this);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_password);
        setTitle("");
        ActivityCollector.addActivity(this);
        //back
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);
        //editText
        email = (EditText) findViewById(R.id.find_password_email);
        username = (EditText) findViewById(R.id.user_name);
        //button
        send = (Button) findViewById(R.id.find_password_send);
        send.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
    /*
    * 判读editText的内容是否不为空
    * @param String userName
    * @param String email
    * return boolean
    */
    private boolean editNotNull(String userName, String email) {
        if (userName.equals("") || email.equals(""))
            PromptShowUtils.showDialog2("警告", "输入不能为空！", this);
        else
            return true;
        return false;

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.find_password_send:

                //获得editText的内容
                Email = email.getText().toString();
                UserName = username.getText().toString();
                boolean isNotNull = editNotNull(UserName, Email);//判断是editText内容否为空
                //当内容不为空，请求查询该用户的注册邮箱
                if (isNotNull){
                    //启动对话框
                    progressDialog = new CustomDialogUtils(this, R.style.CustomDialog);
                    progressDialog.setCancelable(false);        //无法手动取消
                    progressDialog.show();
                    FindPwdUtils.getEmail(Email, UserName, handler);
                }
                break;
        }
    }

}
